package com.hzy.alibaba.config.redisson;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;

/**
 * 手动注入
 */
public class RedissionImportSelector implements ImportSelector, EnvironmentAware {
    private static final Logger log = LoggerFactory.getLogger(RedissionImportSelector.class);
    private static final String CLUSTER = "redisson.nodeAddress";
    private static final String SENTINEL = "redisson.sentinelAddress";
    private Environment environment;

    public RedissionImportSelector() {
    }

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        if (StringUtils.isNotBlank(this.environment.getProperty(CLUSTER))) {
            return new String[]{DistributedLockClusterConfig.class.getName()};
        } else {
            return StringUtils.isNotBlank(this.environment.getProperty(SENTINEL)) ?
                    new String[]{DistributedLockSentinelConfig.class.getName()} :
                    new String[]{DistributedLockSingleConfig.class.getName()};
        }
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
}
